<?php
/**
 * alltosun.com  push_group.php
 * ============================================================================
 * 版权所有 (C) 2009-2013 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 祝利柯 (zhulk@alltosun.com) $
 * $$Id: push_group.php 217468 2015-03-19 06:34:39Z sunxs $$
 */

class Action
{
    /**
     * 推送角色组列表
     * @param unknown_type $action
     * @param unknown_type $params
     */
    public function __call($action = '', $params = array())
    {

        $filter['id >'] = 0;
        $list = _model('push_group')->getList($filter);

        Response::assign('list', $list);
        Response::assign('total', count($list));

        Response::display('admin/group/list.html');
    }

    /**
     * 添加推送角色组
     */
    public function add()
    {
        $push_group_action = '';

        $id = tools_helper::get('id', 0);
        if ($id) {

            Response::assign('action', 'edit');
            $info = _uri('push_group', $id);

            $push_group_info = _model('push_group_action')->getList(array('push_group_id'=>$id));
            if ($push_group_info) {
                foreach ($push_group_info as $k=>$v) {
                    $push_group_action[] = $v['action_id'];
                }
            }

            if ($info['is_root'] != 1) {
                $info['push_group_action'] = $push_group_action;
            }

            Response::assign('info', $info);
        }else {
            Response::assign('action', 'add');
        }

        // $action_list = action_helper::get_all_action_list();

        // Response::assign('action_list', $action_list);
        Response::display('admin/group/add.html');
    }

    /**
     * 保存角色组
     */
    public function save()
    {
        $id   = tools_helper::post('id', 0);
        $data = tools_helper::post('info', array());
        foreach ($data as $k => $v) {
            if (!is_numeric($v)) {
                $data[$k] = AnFilter::filter_string(trim($v));
            }
        }

        $exist_names = _model('push_group')->getFields('name', array(1=>1));
        $exist_names = array_unique($exist_names);
        if (in_array($data['name'], $exist_names)) {
            return array('保存成功', 'success', AnUrl('push/admin/group&t=exist'));
        }

        if ($id) {
            _model('push_group')->update($id, $data);

            return array('更新成功', 'success', AnUrl('push/admin/group'));
        } else {
            $data['add_time'] = date('Y-m-d H:i:s');
            _model('push_group')->create($data);

            return array('保存成功', 'success', AnUrl('push/admin/group'));
        }
    }

    /**
     * 删除组
     * @return string
     */
    public function delete()
    {
        $id = Request::Get('id', 0);

        if (!$id) {
            return "要删除的ID不存在!";
        }

        $info = _uri('push_group', $id);
        if (!$info) {
            return "该推送组不存在!";
        }

        $user_total = _model('push_group_user')->getTotal(array('push_group_id'=>$id));
        if ($user_total) {
            return '请先删除当前推送组下的用户!';
        }

        _model('push_group')->delete(array('id'=>$id));
        // _widget('log')->record('push_group', $id, '删除');
        return 'ok';
    }
}
?>